package com.atguigu.seckill.controller;



import ch.qos.logback.core.util.TimeUtil;
import com.atguigu.seckill.pojo.User;
import com.atguigu.seckill.service.serviceImp.GoodsServiceImpl;
import com.atguigu.seckill.vo.GoodsVo;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Controller
@Slf4j
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    //手动渲染
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private GoodsServiceImpl goodsService;
    /*商品列表页面*/
    @RequestMapping(value = "/toList",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String  toList(Model model , User user, HttpServletRequest request, HttpServletResponse response){
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        String  list = (String)operations.get("goodsList");
        //redis获取goodsList的值
        if(!StringUtils.isEmpty(list)){
            return list;
        }
        List<GoodsVo> goodsList=goodsService.getGoodsList();
        model.addAttribute("user",user);
        model.addAttribute("goodsList",goodsList);
        //之前redis中没有获取到goodsList的值，需要存储到redis中
        WebContext context = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        list = thymeleafViewResolver.getTemplateEngine().process("goodsList", context);
        if (!StringUtils.isEmpty(list)){
            operations.set("goodsList",list,60, TimeUnit.SECONDS);
        }
        return list;
    }

    /*商品详情页*/
    @RequestMapping(value = "/toDetail/{goodsId}",produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toDetail(@PathVariable Long goodsId, Model model,HttpServletRequest request,HttpServletResponse response){
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        String  list = (String)operations.get("goodsDetail");
        //redis获取goodsList的值
        if(!StringUtils.isEmpty(list)){
            return list;
        }
        GoodsVo goodsDetail = goodsService.getGoodsDeatil( goodsId);
        //设置秒杀状态，初始化为0
        int secKillStatus=0;
        //秒杀倒计时
        int remainSeconds=0;
        //当前时间
        Date nowdate=new Date();
        Date startDate=goodsDetail.getStartDate();
        Date endDate =goodsDetail.getEndDate();
        //System.out.println(nowdate+"  "+startDate+"  "+endDate);

        //当前日期在秒杀时间前，说明还未进行秒杀,设置一个秒杀开始倒计时
        if(nowdate.before(startDate)){remainSeconds=(int) (startDate.getTime()-nowdate.getTime())/1000;}
        //秒杀结束
        else if(nowdate.after(endDate)){secKillStatus=2;remainSeconds=-1;}
        //秒杀中
        else {secKillStatus=1;remainSeconds=0;}

        model.addAttribute("secKillStatus",secKillStatus);
        model.addAttribute("remainSeconds",remainSeconds);
        model.addAttribute("goods",goodsDetail);
        //之前redis中没有获取到goodsList的值，需要存储到redis中
        WebContext context = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        list = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", context);
        if (!StringUtils.isEmpty(list)){
            operations.set("goodsDetail",list,60, TimeUnit.SECONDS);
        }
        return list;
        //return "goodsDetail";
    }

}
